package com.example.verifysdk.compare;

import android.content.Context;

import com.example.verifysdk.BaseJson;
import com.example.verifysdk.Contents;
import com.example.verifysdk.R;
import com.example.verifysdk.UploadHelper;
import com.google.gson.Gson;


import java.io.File;
import java.io.IOException;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * Created by LiuV on 2017/4/25.
 */

public class ComparePresenter {
    private static final String TAG = "ComparePresenter";

    private CompareView mCompareView;
    private Gson gson = new Gson();
    private RegisterObjBean mRegisterObjBean;

    public void setmCompareView(CompareView mCompareView) {
        this.mCompareView = mCompareView;
    }

    //多张图片上传
    public void uploadIdAndAllImg(String partnerId, String userId, Map<File, String> imgFileMap){
        UploadHelper.uploadAllImage(partnerId, userId, imgFileMap, mCompareView.getViewContext(), new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                mCompareView.uploadImgFail(e);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException{
                String res = response.body().string();
                if (res.contains(Contents.ERROR_TAG_1) || !res.contains(Contents.ERROR_TAG_2)) {
                    this.onFailure(call, new IOException("服务器响应异常"));
                    return;
                }
                BaseJson baseJson = gson.fromJson(res, BaseJson.class);
                if (baseJson == null || baseJson.getCode() != 0) {
                    mCompareView.uploadImgFail(
                            new Exception(baseJson == null
                                    ? mCompareView.getViewContext().getString(R.string.on_response_unknow_error)
                                    : baseJson.getError()));
                } else {
                    mRegisterObjBean = gson.fromJson(baseJson.getObj(), RegisterObjBean.class);
                    mCompareView.uploadAllImgSuccess(mRegisterObjBean.getName(), mRegisterObjBean.getId(), mRegisterObjBean.isPartnerReturnInformation());
                }
            }
        });
    }

    public void infoIsOk(){
        new CompareModel().infoIsOk(mCompareView.getViewContext(), mRegisterObjBean.getToken(), new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                mCompareView.onFail(e);

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String res = response.body().string();
                if (res.contains(Contents.ERROR_TAG_1 ) || !res.contains(Contents.ERROR_TAG_2)) {
                    this.onFailure(call, new IOException("服务器响应异常"));
                    return;
                }
                BaseJson baseJson = gson.fromJson(res, BaseJson.class);
                if (baseJson == null || baseJson.getCode() != 0) {
                    mCompareView.uploadImgFail(
                            new Exception(baseJson == null
                                    ? mCompareView.getViewContext().getString(R.string.on_response_unknow_error)
                                    : baseJson.getError()));
                } else {
                    mCompareView.registSuccess(mRegisterObjBean);

                }
            }
        });
    }

    interface CompareView {

        void uploadAllImgSuccess(String name, String id, boolean returnId);
        void uploadImgFail(Exception e);
        void onFail(Exception e);
        void registSuccess(RegisterObjBean bean);
        Context getViewContext();
    }
}
